﻿#pragma once

#include "OCMain/Log.h"
#include "OCGIS/GDALCPP.h"
#include "ogr_spatialref.h"
#include "OCVolume/VolumeD3Writer.h"
#include "OCVolume/VolumeGridData.h"

using namespace OCPP;


void csv2json()
{
	OCPP_LOG_INFO << "csv2json\n";

	String fileName = "E:\\volume\\海南\\提供数据20240812\\0721海南下投探空数据\\Z_UPAR_C_BABJ_20240721033920_O_TEMP-DR-PETREL1_20240721032125.csv";
	//String fileName = "E:\\volume\\海南\\提供数据20240812\\台风路径1\\2024_TY2404.csv";
	std::ifstream ifs(U2A(fileName));
	StringVector headers;
	String line;
	getline(ifs, line);
	line = A2U8(line);
	StringVector hsv = StringUtil::split(line, ",");
	Json::Value root;
	size_t lineCount = 0;

	while (getline(ifs, line))
	{
		StringVector sv = StringUtil::split(line, ",");
		if (sv.size() == hsv.size())
		{
			String province = U2A(sv[4]);
			Json::Value obj;
			for (size_t i = 0; i < hsv.size(); i++)
			{
				String hn = hsv[i];
				obj[hn] = sv[i];
			}
			root.append(obj);
		}
		else
		{
			OCPP_LOG_INFO << lineCount << " error row.\n";
		}
		lineCount++;
	}

	String outFileName = U2A(fileName) + ".json";
	std::ofstream ofs(outFileName);
	ofs << ServerUtil::jsonToString(root, "   ");
}


void csv2json2()
{
	OCPP_LOG_INFO << "csv2json\n";

	String fileName = "I:\\Project\\2024\\海南\\1、地面自动站\\surface.csv";
	//String fileName = "E:\\volume\\海南\\提供数据20240812\\台风路径1\\2024_TY2404.csv";
	std::ifstream ifs(U2A(fileName));
	StringVector headers;
	String line;
	getline(ifs, line);
	StringVector hsv = StringUtil::split(line, ",");
	Json::Value root;
	size_t lineCount = 0;

	while (getline(ifs, line))
	{
		StringVector sv = StringUtil::split(line, ",");
		if (sv.size() == hsv.size())
		{
			String province = U2A(sv[4]);
			if (province == U2A("海南省"))
			{
				Json::Value obj;
				for (size_t i = 0; i < hsv.size(); i++)
				{
					String hn = hsv[i];
					if (hn == "Station_Id_C"
						|| hn == "Year"
						|| hn == "Lon"
						|| hn == "Lat"
						|| hn == "Mon"
						|| hn == "Day"
						|| hn == "Hour"
						|| hn == "TEM"
						|| hn == "RHU"
						|| hn == "WIN_S_INST"
						|| hn == "WIN_D_INST")

					{

						obj[hn] = sv[i];
					}
				}

				String id = obj["Station_Id_C"].asString();
				int Year = std::stoi(obj["Year"].asString());
				int Mon = std::stoi(obj["Mon"].asString());
				int Day = std::stoi(obj["Day"].asString());
				int Hour = std::stoi(obj["Hour"].asString());
				String dateTime = str_format("%d-%02d-%02dT%02d:00:00", Year, Mon, Day, Hour);

				Json::Value obj2;
				obj2["TEM"] = obj["TEM"];
				obj2["RHU"] = obj["RHU"];
				obj2["WIN_S_INST"] = obj["WIN_S_INST"];
				obj2["WIN_D_INST"] = obj["WIN_D_INST"];
				root[id]["Lon"] = obj["Lon"];
				root[id]["Lat"] = obj["Lat"];
				root[id]["data"][dateTime] = obj2;
			}
		}
		else
		{
			OCPP_LOG_INFO << lineCount << " error row.\n";
		}
		lineCount++;
	}

	String outFileName = U2A(fileName) + ".json";
	std::ofstream ofs(outFileName);
	ofs << ServerUtil::jsonToString(root, "   ");
}

void csv2json1()
{
	OCPP_LOG_INFO << "csv2json\n";

	String fileName = "I:\\Project\\2024\\海南\\3、探空观测数据\\upar.csv";
	std::ifstream ifs(U2A(fileName));
	StringVector headers;
	String line;
	getline(ifs, line);
	StringVector hsv = StringUtil::split(line, ",");
	Json::Value root;
	size_t lineCount = 0;

	std::map<String, std::map<String, Json::Value> > objMap;

	while (getline(ifs, line))
	{
		StringVector sv = StringUtil::split(line, ",");
		if (sv.size() == hsv.size())
		{
			String stationIdc = sv[0];

			if (stationIdc == "59758"
				|| stationIdc == "59948"
				|| stationIdc == "59981")
			{
				Json::Value obj;
				for (size_t i = 0; i < hsv.size(); i++)
				{
					String hn = hsv[i];
					obj[hn] = sv[i];
				}
				int time_dev_wq = std::atoi(obj["Time_Dev_WQ"].asString().c_str());
				Date date = Date::fromDbStringLocal(obj["Datetime"].asString());
				String datetime = date.after(time_dev_wq).toCustomedFormattedStringLocal("%Y%m%dT%H%M%S");
				objMap[stationIdc][datetime]["WIN_S"] = obj["WIN_S"];
			}
		}
		else
		{
			OCPP_LOG_INFO << lineCount << " error row.\n";
		}
		lineCount++;
	}

	String path = FileUtil::getFilePath(fileName);
	for (const auto& [first, second] : objMap)
	{
		Json::Value obj;
		for (const auto& [dateTime, secondObj] : second)
		{
			obj[dateTime].append(secondObj);
		}
		String outFileName = U2A(path + "/" + first + ".json");
		std::ofstream ofs(outFileName);
		ofs << ServerUtil::jsonToString(obj, "   ");
	}
	//{
	//	std::ofstream ofs();
	//	ofs << ServerUtil::jsonToString(root["59758"]);
	//}
	//{
	//	std::ofstream ofs(U2A(path + "/59948.json"));
	//	ofs << ServerUtil::jsonToString(root["59948"]);
	//}
	//{
	//	std::ofstream ofs(U2A(path + "/59981.json"));
	//	ofs << ServerUtil::jsonToString(root["59981"]);
	//}
}